/*
 * Copyright (c) 2009-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/gfx/IRenderTarget2D.h"
#include "alkes/gfx/IGraphics2D.h"

namespace alkes {

IRenderTarget2D::IRenderTarget2D(IGraphics2D* owner, TargetBehavior target_behavior)
: owner_(owner)
, target_behavior_(target_behavior)
, opacity_(AL_OPACITY_MAX)
{
}

IRenderTarget2D::~IRenderTarget2D()
{
}

bool IRenderTarget2D::correctTextureMapping() const
{
    return owner_->correctTexelMapping();
}

IGraphics2D* IRenderTarget2D::getIGraphics2D() const
{
    return owner_;
}

TargetBehavior IRenderTarget2D::getTargetBehavior() const
{
    return target_behavior_; 
}

void IRenderTarget2D::snapshot(ImageWriter& writer, const Rect* rect)
{
    Rect target_rect(getTargetRect());
    Rect tmp(!rect ? target_rect : *rect);

    if (!rect)
        tmp.intersectWith(target_rect);

    AL_PRE_COND(!tmp.isNull(), );

    onSnapshot(writer, tmp);
}

bool IRenderTarget2D::drawShape(Shape* shape, uint32_t start, uint32_t count)
{
    if (!shape || !count)
    {
        return false;
    }

    bool ret = false;

    if (shape->isInstanceOf(ImageShape::classID()))
    {
        ret = drawShape(static_cast<ImageShape*>(shape), start, count);
    }
    else if (shape->isInstanceOf(RectangleShape::classID()))
    {
        ret = drawShape(static_cast<RectangleShape*>(shape), start, count);
    }
    else if (shape->isInstanceOf(LineShape::classID()))
    {
        ret = drawShape(static_cast<LineShape*>(shape), start, count);
    }
    else if (shape->isInstanceOf(PolylineShape::classID()))
    {
        ret = drawShape(static_cast<PolylineShape*>(shape), start, count);
    }

    return ret;
}

void IRenderTarget2D::translate(const Vector2D& offset)
{
    translate(offset.x, offset.y);
}

void IRenderTarget2D::translateForImage(float x, float y)
{
    translate(x, y);
}

void IRenderTarget2D::scale(float scaling, const Vector2D& origin)
{
    scale(scaling, scaling, origin);
}

}
